package com.bobgao.my_first_demo.controller;

import com.bobgao.my_first_demo.req.LoginUserSaveReq;
import com.bobgao.my_first_demo.req.UserLoginReq;
import com.bobgao.my_first_demo.resp.CommonResp;
import com.bobgao.my_first_demo.resp.UserLoginResp;
import com.bobgao.my_first_demo.service.LoginUserService;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/login-user")
public class LoginUserController {

    @Resource
    private LoginUserService loginUserService;

    @PostMapping("register")
    public CommonResp register(@Valid @RequestBody  LoginUserSaveReq req){
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        loginUserService.register(req);
        return resp;
    }

    @PostMapping("login")
    public CommonResp login(@Valid @RequestBody UserLoginReq req){
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp<UserLoginResp> resp = new CommonResp<>();
        UserLoginResp userLoginResp = loginUserService.login(req);
        resp.setContent(userLoginResp);
        return resp;
    }
}
